import { config as recipeConfig } from '../Items/recipeConfig';
import { d_shop_items } from 'xlsx/shop_items';

// 商店功能模块，提供ui商店的初始数据
export default class GameShop {
    constructor() {
        // this.initShopItems();
    }
    initShopItems() {
        let cfgs = {
            base: [],
            recipe: [],
        };

        for (let cfg of d_shop_items) {
            let item: ShopItemModel = { ...cfg };
            if (cfg.CanBuy == 1) {
                if (cfg.Name.includes('(卷轴)')) {
                    for (let pid in recipeConfig) {
                        for (let id in recipeConfig[pid]) {
                            if (id == cfg.id) {
                                item.recipe = this.getRecipe(pid);
                                break;
                            }
                        }
                    }
                }
                // 添加商品到基本商店
                cfgs.base.push(item);
            } else {
                // 合成公式展示商店
                let item: ShopItemModel = { ...cfg };
                //todo : 合成公式在表格中进行配置
                item.recipe = this.getRecipe(item.id);
                cfgs.recipe.push(item);
            }
        }
        cfgs.base = cfgs.base.sort(this.sortFunc);
        cfgs.recipe = cfgs.recipe.sort(this.sortFunc);

        for (let key in cfgs) {
            // 商店界面.添加物品到商店(key, 商店配置[key]);
            for (let item of cfgs[key]) {
                UIManager.UIShopInstance.model.addItemToSoldList(
                    key as shopItemTypes,
                    item
                );
            }
        }
    }
    sortFunc(a: ShopItemModel, b: ShopItemModel) {
        return a.goldcost - b.goldcost;
    }
    getRecipe(id: string) {
        let config = recipeConfig[id];
        // print_r(config);
        if (config == null) return '';
        let all = GlobalDB.ItemDB.allEquipsMap;
        let arr = [];
        for (let key in config) {
            arr.push(all[key].Name + ' x ' + config[key]);
        }
        return '合成 = ' + arr.join(' + ');
    }
}
